home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 14 / TSTACK.H < prev    next >
C/C++ Source or Header  |  1995-02-23  |  3KB  |  117 lines

  1. // File from page 598 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: TSTACK.H -- Stack using templates
  34. #ifndef TSTACK_H_
  35. #define TSTACK_H_
  36.  
  37. // Some implementations require this:
  38. template<class T> class tstackIterator;
  39.  
  40. template<class T>
  41. class tstack {
  42.   struct link {
  43.     T* data;
  44.     link* next;
  45.     link(T* Data, link* Next) {
  46.       data = Data;
  47.       next = Next;
  48.     }
  49.   } * head;
  50.   int owns;
  51. public:
  52.   tstack(int Owns = 1) : head(0), owns(Owns) {}
  53.   ~tstack();
  54.   void push(T* Data) {
  55.     head = new link(Data,head);
  56.   }
  57.   T* peek() const { return head->data; }
  58.   T* pop();
  59.   int Owns() const { return owns; }
  60.   void Owns(int newownership) {
  61.     owns = newownership;
  62.   }
  63.   friend class tstackIterator<T>;
  64. };
  65.  
  66. template<class T>
  67. T* tstack<T>::pop() {
  68.   if(head == 0) return 0;
  69.   T* result = head->data;
  70.   link* oldHead = head;
  71.   head = head->next;
  72.   delete oldHead;
  73.   return result;
  74. }
  75.  
  76. template<class T>
  77. tstack<T>::~tstack() {
  78.   link* cursor = head;
  79.   while(head) {
  80.     cursor = cursor->next;
  81.     // Conditional cleanup of data:
  82.     if(owns) delete head->data;
  83.     delete head;
  84.     head = cursor;
  85.   }
  86. }
  87.  
  88. template<class T>
  89. class tstackIterator {
  90.   tstack<T>::link* p;
  91. public:
  92.   tstackIterator(const tstack<T>& tl)
  93.     : p(tl.head) {}
  94.   tstackIterator(const tstackIterator& tl)
  95.     : p(tl.p) {}
  96.   // operator++ returns boolean indicating end:
  97.   int operator++() {
  98.     if(p->next)
  99.       p = p->next;
  100.     else p = 0; // Indicates end of list
  101.     return int(p);
  102.   }
  103.   int operator++(int) { return operator++(); }
  104.   // Smart pointer:
  105.   T* operator->() const {
  106.     if(!p) return 0;
  107.     return p->data;
  108.   }
  109.   T* current() const {
  110.     if(!p) return 0;
  111.     return p->data;
  112.   }
  113.   // int conversion for conditional test:
  114.   operator int() const { return p ? 1 : 0; }
  115. };
  116. #endif // TSTACK_H_
  117.